#!/usr/bin/python
import os, sys, subprocess, tempfile, pipes, getopt
from optparse import OptionParser

def main(indir, outdir, pp3, type="tiff", overwrite=False):
	
	command = ["rawtherapee"]
	command.extend(["-o", outdir])
	command.extend(["-p", pp3])
	if type == "tiff":		
		command.append("-t1")
	elif type == "jpeg":
		command.append("-j100")
	if overwrite == True:
		command.append("-Y")
	command.extend(["-c", indir])

	subprocess.call(command, stdout=open("/dev/null", "w"))

def str2bool(v):
	return v.lower() in ("yes", "true", "t", "1")

if __name__ == "__main__":
	indir = ''
	outdir = ''
	pp3 = os.path.join((os.path.dirname(__file__)), "invert.pp3")
	type = ''
	overwrite = ''
	
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hi:o:c:t:r:", ["indir=", "outdir=", "config=", "type=", "overwrite="])
	except getopt.GetoptError:
		print 'raw.pyt -i <inputdir> -o <outputdir>, -t <outputfiletype> -r<overwrite>'
		sys.exit(2)
		
	for opt, arg in opts:
		if opt == '-h':
			print 'raw.py -i <inputdir> -o <outputdir>, -t <outputfiletype> -r <overwrite>'
			sys.exit()
		elif opt in ("-i", "--indir"):
			indir = arg
		elif opt in ("-o", "--outdir"):
			outdir = arg
		elif opt in ("-c", "--config"):
			pp3 = arg
		elif opt in ("-t", "--type"):
			type = arg.lower()
		elif opt in ("-r", "--overwrite"):
			overwrite = str2bool(arg)
	
	main(indir, outdir, pp3, type, overwrite)

